/*
 * bsp_aes_ccm.h
 *
 *  Created on: 2023年7月5日
 *      Author: fjx
 */

#ifndef BSP_AES_CCM_H_
#define BSP_AES_CCM_H_
#include "psa/crypto.h"

#define DYNAMIC_AES_CCM_KEY_ID                      0x7ffffffe
typedef enum {
  DS_AES_CCM_STATUS_SUCCESS = 0,
  DS_AES_CCM_STATUS_FAIL = -1,
  DS_AES_CCM_STATUS_ERROR_NOT_SUPPORTED = -2,
  DS_AES_CCM_STATUS_KEY_FULL = -3,

}DS_AES_CCM_STATUS;

typedef enum {
  CCM_TAG_LEN_4 = 4,  //Default
  CCM_TAG_LEN_6 = 6,
  CCM_TAG_LEN_8 = 8,
  CCM_TAG_LEN_10 = 10,
  CCM_TAG_LEN_12 = 12,
  CCM_TAG_LEN_14 = 14,
  CCM_TAG_LEN_16 = 16,
  //4, 6, 8, 10, 12, 14, and 16 (Default 4)
}CCM_Tag_Len_t;

/*
 *@brief 设置tag的长度,默认为4Byte
 *@param len tag的长度，长度可以为4,6,8,10,12,14,16
 *
 */
psa_algorithm_t DS_aes_ccm_set_tag (CCM_Tag_Len_t len );
/*
 *@brief 导入新的key
 *@param key key数据
 *@param key key的长度
 */
DS_AES_CCM_STATUS DS_aes_ccm_import_key (const uint8_t *key, size_t key_len );


/*
 *@brief aes ccm 加密
 *@param nonce  nonce数据
 *@param nonce  nonce数据长度
 *@param additional_data additional数据
 *@param additional_data_length additional数据长度
 *@param plaintext  明文数据
 *@param plaintext_length 明文数据长度
 *@param ciphertext  里包含密文和tag,tag的长度默认为4Byte。
 *                   如果需要修改tag的长度，使用DS_aes_ccm_set_tag设置长度
 *@param ciphertext_size 密文buffer所用额长度
 *@param ciphertext_length 返回密文和tag总的长度
 *@return int 0 OK； -1 failed ; -2 not supported.
 *
*/
DS_AES_CCM_STATUS DS_aes_ccm_ae(const uint8_t *nonce,
                           size_t nonce_length,
                           const uint8_t *additional_data,
                           size_t additional_data_length,
                           const uint8_t *plaintext,
                           size_t plaintext_length,
                           uint8_t *ciphertext,
                           size_t ciphertext_size,
                           size_t *ciphertext_length );

/*
 *@brief aes ccm 解密
 *@param nonce  nonce数据
 *@param nonce  nonce数据长度
 *@param additional_data additional数据
 *@param additional_data_length additional数据长度
 *@param ciphertext  里包含密文和tag,tag的长度默认为4Byte。
 *                   如果需要修改tag的长度，使用DS_aes_ccm_set_tag设置长度
 *@param ciphertext_length 密文和tag总的长度
 *@param plaintext 解密后的明文
 *@param plaintext_length 解密后的铭文长度
 *@return int 0 OK； -1 failed ; -2 not supported.
 */
DS_AES_CCM_STATUS DS_aes_ccm_de(const uint8_t *nonce,
                           size_t nonce_length,
                           const uint8_t *additional_data,
                           size_t additional_data_length,
                           uint8_t *ciphertext,
                           size_t ciphertext_size,
                           uint8_t *plaintext,
                           size_t plaintext_length,
                           size_t *ciphertext_length );
/*
 *
 *
 *
 */
DS_AES_CCM_STATUS DS_aes_ccm_use_keyid_ae(psa_key_id_t keyid,
                           const uint8_t *nonce,
                           size_t nonce_length,
                           const uint8_t *additional_data,
                           size_t additional_data_length,
                           const uint8_t *plaintext,
                           size_t plaintext_length,
                           uint8_t *ciphertext,
                           size_t ciphertext_size,
                           size_t *ciphertext_length );
/*
 *
 *
 *
 */
DS_AES_CCM_STATUS DS_aes_ccm_use_keyid_de(psa_key_id_t keyid,
                           const uint8_t *nonce,
                           size_t nonce_length,
                           const uint8_t *additional_data,
                           size_t additional_data_length,
                           uint8_t *ciphertext,
                           size_t ciphertext_size,
                           uint8_t *plaintext,
                           size_t plaintext_length,
                           size_t *ciphertext_length );
/*
 *@brief 获取当前key id
 *@return 返回key id值
 */
psa_key_id_t DS_aes_ccm_get_key_id (void);



/*
 *@brief aes ccm 初始化
 *@param key key的buffer
 *@param key key长度
 *
 */
DS_AES_CCM_STATUS DS_aes_ccm_init (uint8_t *key, size_t key_len );


void DS_aes_ccm_destroy_keyid (uint32_t _key_id);

psa_key_id_t DS_aes_ccm_get_dynamic_key_id (void);
uint8_t DS_aes_ccm_dynamic_init(const uint8_t *key,size_t key_len);
//psa_status_t DS_aese_ccm_get_key(void);

#endif /* DS_AES_CCM_H_ */
